//install map packages if not already
ssc install maptile
ssc install spmap

net install cleanplots, from("https://tdmize.github.io/data/cleanplots")
set scheme cleanplots, perm
maptile_install using "http://files.michaelstepner.com/geo_statehex.zip", replace

//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
//the above is just the path to the replcation files. The data from here on are 
//hierarchical and nested from this file path. 

//read in the state dta file 
use "data\nyt_states.dta", clear


append using "data\scratch\hour60"
sort state hoursfromclose


** drop two dummy rows that had been added before
drop if hoursfromclose == 60
drop if hoursfromclose > 60.00999 & hoursfromclose < 60.01 // 60.01 isn't precisely represented
drop if hoursfromclose == 6

//drop maxtotal field from earlier data 
drop maxtotal

** calculate reporting momentum

gen total4 = norm_total if hoursfromclose <= 4 & hoursfromclose[_n+1] > 4
gen total8 = norm_total if hoursfromclose <= 8 & hoursfromclose[_n+1] > 8
gen total24 = norm_total if hoursfromclose <= 24 & hoursfromclose[_n+1] > 24
gen total48 = norm_total if hoursfromclose <= 48 & hoursfromclose[_n+1] > 48

egen maxtotal4 = max(total4),by(state)
egen maxtotal8 = max(total8),by(state)
egen maxtotal24 = max(total24),by(state)
egen maxtotal48 = max(total48),by(state)



keep if state ~= state[_n+1]
replace maxtotal4 = 94.41113 if state == "hawaii"


gsort -maxtotal4
gen order = _n

gen state_lbl = proper(subinstr(state,"-"," ",.))
replace state_lbl = "D.C." if state_lbl == "District Of Columbia"
drop state
rename state_lbl state
merge 1:1 state using "data\state_codes", nogen
drop state
rename state_po state
replace state = "DC" in 8
drop in 52


maptile maxtotal4,geo(statehex) cutvalues(50 70 90) rangecolor(navy*.001 navy*1.2) twopt( title(After 4 hours)  legend(order(5 "90-100" 4 "70-90" 3 "50-70" 2 "9-50") title(Percent reported,size(*.8))) )
graph export "figures\fig4a.png", replace width(2500)
maptile maxtotal8,geo(statehex) cutvalues(50 70 90) rangecolor(navy*.001 navy*1.2) twopt( title(After 8 hours)  legend(order(5 "90-100" 4 "70-90" 3 "50-70" 2 "9-50") title(Percent reported,size(*.8))) )
graph export "figures\fig4b.png", replace width(2500)
maptile maxtotal24,geo(statehex) cutvalues(50 70 90) rangecolor(navy*.001 navy*1.2) twopt( title(After 24 hours)  legend(order(5 "90-100" 4 "70-90" 3 "50-70" 2 "9-50") title(Percent reported,size(*.8))) )
graph export "figures\fig4c.png", replace width(2500)
maptile maxtotal48,geo(statehex) cutvalues(50 70 90) rangecolor(navy*.001 navy*1.2) twopt( title(After 48 hours)  legend(order(5 "90-100" 4 "70-90" 3 "50-70" 2 "9-50") title(Percent reported,size(*.8))) )
graph export "figures\fig4d.png", replace width(2500)
